﻿Public Class register_login
    Inherits System.Web.UI.Page
    Private thisUser As New user
    Private objDB = New DBConnect

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        lblRegisterError.Visible = False
        lblLoginError.Visible = False

        If Not IsPostBack Then
            If Session("db") Is Nothing Then
                Session("db") = objDB
            End If
            If Session("user") Is Nothing Then
                Session("user") = thisUser
            End If
            objDB = Session("db")
            thisUser = Session("user")
        End If
    End Sub

    Protected Sub btnRegSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRegSubmit.Click
        'on click this sub will collect all data from form fields and copy to thisUse
        Dim sql As String = ""
        Dim isValid As Boolean = True
        'validation
        'make sure not blank 
        If txtFirstName.Text = "" Then
            lblFirstNameValidator.Visible = True
            lblFirstNameValidator.Text = "*"
            lblFirstNameValidator.ForeColor = Drawing.Color.Red
            isValid = False
            lblRegisterError.Visible = True
            lblRegisterError.Text = "Please Enter a First Name"
            'lblMsg.Visible = True
        Else
            lblFirstNameValidator.Visible = False
            ' lblMsg.Visible = False
        End If

        If txtLastName.Text = "" Then
            lblLastNameValidator.Visible = True
            lblLastNameValidator.Text = "*"
            lblLastNameValidator.ForeColor = Drawing.Color.Red
            isValid = False
            lblRegisterError.Visible = True
            lblRegisterError.Text = "Please Enter a Last Name"
            'lblMsg.Visible = True
        Else
            lblLastNameValidator.Visible = False
            'lblMsg.Visible = False
        End If
        Dim count As Integer = 0
        sql = "SELECT * FROM Participant WHERE Pseudonym = '" & txtPseudonym.Text & "';"
        objDB.getDataSet(sql, count)
        If txtPseudonym.Text = "" Then
            lblPseudonymValidator.Visible = True
            lblPseudonymValidator.Text = "*"
            lblPseudonymValidator.ForeColor = Drawing.Color.Red
            isValid = False
            lblRegisterError.Visible = True
            lblRegisterError.Text = "Please Enter a Pseudonym"
        ElseIf (count <> 0) Then
            lblPseudonymValidator.Visible = True
            lblPseudonymValidator.Text = "*"
            lblPseudonymValidator.ForeColor = Drawing.Color.Red
            isValid = False
            lblRegisterError.Visible = True
            lblRegisterError.Text = "Pseudonym is Already in Use, Please Change"
        Else
            lblPseudonymValidator.Visible = False
            ' lblMsg.Visible = False
        End If

        If txtPassword.Text = "" Then
            lblPasswordValidator.Visible = True
            lblPasswordValidator.Text = "*"
            lblPasswordValidator.ForeColor = Drawing.Color.Red
            isValid = False
            lblRegisterError.Visible = True
            lblRegisterError.Text = "Please Enter a Password"
        Else
            lblPasswordValidator.Visible = False
        End If

        'collect data from form
        If isValid Then
            thisUser.firstname = txtFirstName.Text
            thisUser.lastname = txtLastName.Text
            thisUser.pseudonym = txtPseudonym.Text
            thisUser.password = txtPassword.Text


            sql = "INSERT INTO Participant (FirstName, LastName, Pseudonym, Password) Values('" & thisUser.firstname & "','" & thisUser.lastname & "','" & thisUser.pseudonym & _
                "','" & thisUser.password & "')" 'SQL insert String


            objDB.doUpdate(sql) 'send string to database
            Response.Redirect("MainMenu.aspx")
        End If



    End Sub

    Protected Sub btnRegClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnRegClear.Click
        'clears all form fields and data in memory

        thisUser.firstname = ""
        thisUser.lastname = ""
        thisUser.pseudonym = ""
        thisUser.password = ""

        Me.txtFirstName.Text = ""
        Me.txtLastName.Text = ""
        Me.txtPseudonym.Text = ""
        Me.txtPassword.Text = ""
        Session("user") = thisUser
    End Sub

    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
        'submits username and password for authentication
        Dim password As String = ""
        Dim userExist As Integer = 0
        Dim IsValid As Boolean = True
        Dim Sql As String

        'validate 
        If (txtLoginPseudonym.Text = "" Or txtLoginPassword.Text = "") Then
            IsValid = False
            lblLoginError.Visible = True
            lblLoginError.Text = "Please Fill in Blank Fields"
        Else
            Sql = "SELECT * FROM Participant WHERE Pseudonym ='" & txtLoginPseudonym.Text & "'"
            objDB.getDataSet(Sql, userExist)
            If userExist < 1 Then
                IsValid = False
                lblLoginError.Visible = True
                lblLoginError.Text = "The Username Does not Exist"
            End If
        End If

        'authenticate
        If (IsValid) Then
            Sql = "SELECT * FROM Participant WHERE Pseudonym ='" & txtLoginPseudonym.Text & "'"
            objDB.getDataSet(Sql, userExist)

            password = objDB.getField("Password", 0)

            If txtLoginPassword.Text = password Then
                ' user is authenticated
                ' load user into session
                thisUser.firstname = objDB.getfield("Firstname", 0)
                thisUser.lastname = objDB.getField("Lastname", 0)
                thisUser.pseudonym = objDB.getField("Pseudonym", 0)
                thisUser.password = objDB.getField("Password", 0)
                thisUser.participantID = objDB.getField("ParticipantID", 0)
                Session("user") = thisUser

                Response.Redirect("MainMenu.aspx")
            Else
                lblLoginError.Visible = True
                lblLoginError.Text = "The Password You Entered is Incorrect"
            End If
        End If
    End Sub

    Protected Sub btnLoginClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLoginClear.Click
        txtLoginPseudonym.Text = ""
        txtLoginPassword.Text = ""
    End Sub
End Class